//
//  LQEmoticonKeyBoard.swift
//  Weibo
//
//  Created by ios on 16/8/17.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit
private let EmoticonCollectionCell = "EmoticonCollectionCellEmoticonCollectionCell"
class LQEmoticonKeyBoard: UIView {
    
    //  MARK: -- 懒加载控件
    //底部的ToolBar
    private lazy var toolBar: LQEmoticonToolBar = {
        let toolBar = LQEmoticonToolBar(frame: CGRectZero)
        return toolBar
    }()
    
    //表情视图
    private lazy var EmoticonCollection:UICollectionView = {
    let flowlaout = UICollectionViewFlowLayout()
        //设置水平滚动
    flowlaout.scrollDirection = .Horizontal
    let EmoticonView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowlaout)
        //设置分页
    EmoticonView.pagingEnabled = true
        //取除水平和垂直的滚动条
    EmoticonView.showsHorizontalScrollIndicator = false
    EmoticonView.showsVerticalScrollIndicator = false
        //取除弹簧效果
    EmoticonView.bounces = false
    EmoticonView.backgroundColor = RandomColor()
    //设置数据源代理
    EmoticonView.dataSource = self
   //注册cell
    EmoticonView.registerClass(LQEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: EmoticonCollectionCell)
    return EmoticonView
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        //  添加控件
        addSubview(toolBar)
        addSubview(EmoticonCollection)
        //  设置约束
        
         EmoticonCollection.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
            
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.height.equalTo(35)
        }
        
        toolBar.didSeletedButtonClosure = { [weak self] (type: LQEmoticonToolBarButtonType) in
            
            switch type {
            case .Normal:
                print("Normal")
                
            case .Emoji:
                print("emoji")
            case .LXH:
                print("LXH")
                
                
            }
            
        }
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let flowlaout = EmoticonCollection.collectionViewLayout as! UICollectionViewFlowLayout
        flowlaout.itemSize = EmoticonCollection.size
        //设置水平和垂直方向间距为0
        flowlaout.minimumInteritemSpacing = 0
        flowlaout.minimumLineSpacing = 0
    }
    

}

extension LQEmoticonKeyBoard:UICollectionViewDataSource{
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 3
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
       let cell = collectionView.dequeueReusableCellWithReuseIdentifier(EmoticonCollectionCell, forIndexPath: indexPath) as! LQEmoticonCollectionViewCell
        cell.backgroundColor = RandomColor()
        cell.index = indexPath
        return cell
    }

}














